/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mh.bean;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.model.SelectItem;
import mh.bol.AdminComplaintBO;
import mh.entity.ComplaintEn;

/**
 *
 * @author VIETNH
 */
@ManagedBean
@RequestScoped
public class AdminComplaintRequest {

    /** Creates a new instance of ComplaintRequest */
    AdminComplaintBO cBO = new AdminComplaintBO();
    private ComplaintEn ce = null;
    private String cid;
    private String eid;
    private String type;
    private String title;
    private String description;
    private String datepost;
    private String deadline;
    private String status;
    private String active;
    private String viewByStatus = "0";
    private String viewByType = "0";
    private String viewByDeadline = "1";
    private String euserName;
    private String viewByDepartment = "1";

    public String getViewByDepartment() {
        return viewByDepartment;
    }

    public void setViewByDepartment(String viewByDepartment) {
        this.viewByDepartment = viewByDepartment;
    }
    
    public String getEuserName() {
        return euserName;
    }

    public void setEuserName(String euserName) {
        this.euserName = euserName;
    }

    public String getViewByDeadline() {
        return viewByDeadline;
    }

    public void setViewByDeadline(String viewByDeadline) {
        this.viewByDeadline = viewByDeadline;
    }

    public String getViewByStatus() {
        return viewByStatus;
    }

    public void setViewByStatus(String viewByStatus) {
        this.viewByStatus = viewByStatus;
    }

    public String getViewByType() {
        return viewByType;
    }

    public void setViewByType(String viewByType) {
        this.viewByType = viewByType;
    }

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }

    public ComplaintEn getCe() {
        return ce;
    }

    public void setCe(ComplaintEn ce) {
        this.ce = ce;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getDatepost() {
        return datepost;
    }

    public void setDatepost(String datepost) {
        this.datepost = datepost;
    }

    public String getDeadline() {
        return deadline;
    }

    public void setDeadline(String deadline) {
        this.deadline = deadline;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getEid() {
        return eid;
    }

    public void setEid(String eid) {
        this.eid = eid;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    private ArrayList<ComplaintEn> listComp;

    public ArrayList<ComplaintEn> getListComp() {
        return listComp;
    }

    public void setListComp(ArrayList<ComplaintEn> listComp) {
        this.listComp = listComp;
    }
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ValueBinding binding = facesContext.getApplication().createValueBinding("#{loginSession}");
    LoginSession loginSession = (LoginSession) binding.getValue(facesContext);

    public AdminComplaintRequest() {
        checkSession();
        listComp = cBO.viewComplaint(loginSession.getEmpEn().getEtype() + "", loginSession.getEmpEn().getDid() + "", "1", "1", "1", "1",viewByDepartment);
    }

    public void checkSession() {
        if (loginSession.getEmpEn() != null) {
            if (loginSession.getEmpEn().getEtype() == 3) {
                redirect("index.xhtml");
            }
        } else {
            redirect("Login.xhtml");
        }
    }

    public List listTypeDepartment() {
        List selectItems = new ArrayList();
        selectItems.add(new SelectItem("1", "Educational Services"));
        selectItems.add(new SelectItem("2", "Management Services"));
        selectItems.add(new SelectItem("3", "Learning Services"));
        selectItems.add(new SelectItem("4", "Internal Systems"));
        selectItems.add(new SelectItem("5", "Human Resources"));
        return selectItems;
    }
    
    public List listTypeComp() {
        List selectItems = new ArrayList();
        selectItems.add(new SelectItem("0", "View All"));
        selectItems.add(new SelectItem("1", "Facilities"));
        selectItems.add(new SelectItem("2", "Wage"));
        selectItems.add(new SelectItem("3", "Other issues"));
        return selectItems;
    }

    public List listStatusComp() {
        List selectItems = new ArrayList();
        selectItems.add(new SelectItem("0", "Tất cả"));
        selectItems.add(new SelectItem("1", "Đã trả lời"));
        selectItems.add(new SelectItem("2", "Chưa trả lời"));

        return selectItems;
    }

    public List listDeadlineComp() {
        List selectItems = new ArrayList();
        selectItems.add(new SelectItem("0", "Gần đây"));
        selectItems.add(new SelectItem("1", "Xa hơn"));

        return selectItems;
    }

    public void redirect(String page) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(page);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public ArrayList<ComplaintEn> getAllComplaint() {
        return cBO.viewComplaint(loginSession.getEmpEn().getEtype() + "", loginSession.getEmpEn().getDid() + "", "1", "1", "1", "1",viewByDepartment);
    }

    public ArrayList<ComplaintEn> getComplaintByEID() {
        return cBO.getComplaintByEID(eid);
    }

    public String searchComplaintByStatus() {
        listComp = cBO.viewComplaint(loginSession.getEmpEn().getEtype() + "", loginSession.getEmpEn().getDid() + "", "3", "1", viewByStatus, "1",viewByDepartment);
        return "AdListComplaint.xhtml";
    }

    public String searchComplaintByType() {
        listComp = cBO.viewComplaint(loginSession.getEmpEn().getEtype() + "", loginSession.getEmpEn().getDid() + "", "2", viewByType, "1", "1",viewByDepartment);
        return "AdListComplaint.xhtml";
    }

    public String searchComplaintByDeadline() {
        listComp = cBO.viewComplaint(loginSession.getEmpEn().getEtype() + "", loginSession.getEmpEn().getDid() + "", "4", "1", "1", viewByDeadline,viewByDepartment);
        return "AdListComplaint.xhtml";
    }
    
    public String searchComplaintByDepartment() {
        listComp = cBO.viewComplaint(loginSession.getEmpEn().getEtype() + "", loginSession.getEmpEn().getDid() + "", "5", "1", "1", viewByDeadline,viewByDepartment);
        return "AdListComplaint.xhtml";
    }

    public String listComplaintDetailByCID() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
        cid = requestMap.get("cID").toString();
        if (cid != null) {
            listComp = cBO.getComplaintDetailByCID(cid);
            return "AdDetailComplaint.xhtml";
        }
        return "error.xhtml";
    }

    public String deleteComplaint() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
        cid = requestMap.get("cID").toString();
        boolean flag = cBO.deleteComplaint(cid);
        if (flag) {
            redirect("AdListComplaint.xhtml");
            return "AdListComplaint.xhtml";
        } else {
            return "error.xhtml";
        }
    }

    public String replyComplaint() {
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date date = new Date();
        int ngay = date.getDate();
        int thang = date.getMonth();//0-11
        int nam = date.getYear();

        ce = new ComplaintEn();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
        cid = requestMap.get("cID").toString();
        eid = requestMap.get("eID").toString();
        euserName = requestMap.get("euserName").toString();

        ce.setC_ID(cid);
        ce.setE_ID(eid);
        ce.setTitle("BQT " + euserName + " trả lời !");
        ce.setDescription(description);
        ce.setDatePost(dateFormat.format(date));
        date = new Date(nam, thang, ngay + 2);
        ce.setDeadline(dateFormat.format(date));
        ce.setStatus(true);
        if (cBO.replyComplaint(ce)) {
            return "AdListComplaint.xhtml";
        }
        return "error.xhtml";
    }
}
